package protobuf.Serialization;

import component.userInformation.UserAttribute;
import myClient.session.ClientSession;
import protobuf.protoClass.ProtoMsg;

public class LoginRequestMsgConverter extends BaseConverter {

    private final UserAttribute userAttribute;

    /**
     * {@link ProtoMsg.Message} 中 LoginRequest 字段
     */
    public LoginRequestMsgConverter(ClientSession session) {
        super(ProtoMsg.HeadType.LOGIN_REQUEST, session);
        this.userAttribute = session.getUserAttribute();
    }

    /**
     * 静态方法
     *
     * @param clientSession Session
     * @return 登陆请求 protoBuf类型
     */
    public static ProtoMsg.Message build(ClientSession clientSession) {
        return new LoginRequestMsgConverter(clientSession).build();
    }

    /**
     * 设置 ProtoMsg.Message第 4个字段：LoginRequest ，
     * 并调用 build()方法，创建 ProtoMsg.Message
     */
    public ProtoMsg.Message build() {

        //构建 ProtoMsg.LoginRequest
        ProtoMsg.LoginRequest.Builder loginRequestBuilder =
                ProtoMsg.LoginRequest.newBuilder()
                        .setUid(userAttribute.getUid())
                        .setDeviceId(userAttribute.getDeviceID())
                        .setToken(userAttribute.getToken())
                        .setPlatform(userAttribute.getPlatform().ordinal());
        //将信息序列号设置为 -1
        return get_ProtoMsg_Message_Builder_withThreeFields(-1) // ProtoMsg.Message前三个字段
                .setLoginRequest(loginRequestBuilder)   //ProtoMsg.Message第 4个字段：LoginRequest 登录请求
                .build();
    }

}
